package com.sgb.kjwl.view.ui.activity.transport.driver.order;

import android.content.Context;
import android.content.Intent;

import com.google.android.material.tabs.TabLayout;
import com.sgb.capital.callback.Constants;
import com.sgb.capital.callback.ZMessageEvent;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.callback.OnTabSelectedListener;
import com.sgb.kjwl.databinding.ActivityDriverTransportOrderBinding;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LogisticsEvent;
import com.sgb.kjwl.view.ui.fragment.transport.TransportOrderDriverFragment;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

/**
 * Time:2021/2/3
 * Author:黄淋
 * Description:司机运输订单
 */
public class TransportOrderDriverActivity extends AppActivity {
    private ActivityDriverTransportOrderBinding mBinding;
    private ArrayList<Fragment> mFragments;
    private String[] titles = {"全部","待确认","待发车", "运输中", "待支付", "已完成"};

    public static void start(Context context) {
        Intent intent = new Intent(context, TransportOrderDriverActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_driver_transport_order);
        mBinding.actionBar.setOnBackClickListener(view -> finish());
        mFragments = new ArrayList<>();
        showOrderFragment();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    public void showOrderFragment() {
        for (int i = 0; i < titles.length; i++) {
            mFragments.add(TransportOrderDriverFragment.newInstance(i));
        }
        SlidingFragmentPagerAdapter pagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, titles);
        mBinding.vpOrder.setOffscreenPageLimit(titles.length);
        mBinding.vpOrder.setAdapter(pagerAdapter);
        mBinding.tabLayout.setupWithViewPager(mBinding.vpOrder);
        mBinding.tabLayout.addOnTabSelectedListener(new OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                refreshData();
            }
        });
    }
    private void refreshData() {
                ((TransportOrderDriverFragment) mFragments.get(mBinding.tabLayout.getSelectedTabPosition())).refreshData(mBinding.tabLayout.getSelectedTabPosition());

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (mFragments!=null){
            mFragments.clear();
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LogisticsEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_SURE_START_TRANSPORT_SUCCESS) {
            refreshData();
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReFresh(ZMessageEvent event) {
        if (event.code == Constants.EVENT_REFRESH) {
            refreshData();
        }
        if (event.code==Constants.EVENT_PAY_SUCCESS){
            refreshData();
        }
    }
}
